/*
 * CoordinateInfoPanel.java
 *
 * Created on July 1, 2005, 3:05 PM
 */

package ika.gui;

import ika.map.tools.*;
import ika.map.*;
import ika.mapanalyst.LinkManager;
import ika.utils.CoordinateFormatter;
import java.text.*;

/**
 *
 * @author  jenny
 */
public class CoordinateInfoPanel extends javax.swing.JPanel
        implements MeasureToolListener, MapToolMouseMotionListener {
    
    private static final DecimalFormat angleFormatter =
            new DecimalFormat("###,##0.0");
   
    /** Creates new form CoordinateInfoPanel */
    public CoordinateInfoPanel() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        xCoordLabel = new javax.swing.JLabel();
        yCoordLabel = new javax.swing.JLabel();
        xTextLabel = new javax.swing.JLabel();
        yTextLabel = new javax.swing.JLabel();
        distTextLabel = new javax.swing.JLabel();
        distLabel = new javax.swing.JLabel();
        angleTextLabel = new javax.swing.JLabel();
        angleLabel = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        xCoordLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        xCoordLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        xCoordLabel.setToolTipText("Horizontal coordinate of the mouse pointer.");
        xCoordLabel.setPreferredSize(new java.awt.Dimension(90, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        add(xCoordLabel, gridBagConstraints);

        yCoordLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        yCoordLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        yCoordLabel.setToolTipText("Vertical coordinate of the mouse pointer.");
        yCoordLabel.setPreferredSize(new java.awt.Dimension(90, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        add(yCoordLabel, gridBagConstraints);

        xTextLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        xTextLabel.setText("X : ");
        xTextLabel.setToolTipText("Horizontal coordinate of the mouse pointer.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        add(xTextLabel, gridBagConstraints);

        yTextLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        yTextLabel.setText("Y : ");
        yTextLabel.setToolTipText("Vertical coordinate of the mouse pointer.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        add(yTextLabel, gridBagConstraints);

        distTextLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        distTextLabel.setText("Distance : ");
        distTextLabel.setToolTipText("The last distance measured with the Measure Distance and Angle tool.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        add(distTextLabel, gridBagConstraints);

        distLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        distLabel.setText("-");
        distLabel.setToolTipText("The last distance measured with the Measure Distance and Angle tool.");
        distLabel.setPreferredSize(new java.awt.Dimension(80, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        add(distLabel, gridBagConstraints);

        angleTextLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        angleTextLabel.setText("Angle : ");
        angleTextLabel.setToolTipText("The last angle measured with the Measure Distance and Angle tool.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        add(angleTextLabel, gridBagConstraints);

        angleLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        angleLabel.setText("-");
        angleLabel.setToolTipText("The last angle measured with the Measure Distance and Angle tool.");
        angleLabel.setPreferredSize(new java.awt.Dimension(80, 13));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        add(angleLabel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    public void clearDistance() {
        this.distLabel.setText("-");
        this.angleLabel.setText("-");
    }
    
    public void distanceChanged(double distance, double angle,
            ika.gui.MapComponent mapComponent) {
        
        // distance
        CoordinateFormatter coordFormatter = mapComponent.getCoordinateFormatter();
        this.distLabel.setText(coordFormatter.format(distance));
        
        // angle
        double azimuth = -Math.toDegrees(angle) + 90.;
        if (azimuth < 0)
            azimuth += 360;
        String angleStr = angleFormatter.format(azimuth);
        this.angleLabel.setText("<HTML>"+angleStr+"&#176</HTML>");
    }

    public void positionChanged(double x1, double y1, double x2, double y2, MapComponent mapComponent) {
        final double dx = x2 - x1;
        final double dy = y2 - y1;
        final double d = Math.hypot(dx, dy);
        final double angle = Math.atan2(dy, dx);

        /*double[][] localOSMPoints = null;
        if (true) {
            localOSMPoints = linkManager.getLinkedPoints()[1];
            OSMCoordinateConverter.fromOSM(localOSMPoints, localOSMPoints, null);
        }

        System.out.println("distanceChanged");*/
    }

    private void updateCoordinates(java.awt.geom.Point2D.Double point,
            ika.gui.MapComponent mapComponent) {
        if (point != null) {
            CoordinateFormatter formatter = mapComponent.getCoordinateFormatter();
            String xStr = formatter.format(point.getX());
            String yStr = formatter.format(point.getY());
            this.xCoordLabel.setText(xStr);
            this.yCoordLabel.setText(yStr);
        } else {
            this.xCoordLabel.setText("-");
            this.yCoordLabel.setText("-");
        }
    }
    
    public void mouseMoved(java.awt.geom.Point2D.Double point,
            ika.gui.MapComponent mapComponent) {
        this.updateCoordinates(point, mapComponent);
    }
    
    public void registerWithMapComponent(MapComponent mapComponent) {
        mapComponent.addMouseMotionListener(this);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel angleLabel;
    private javax.swing.JLabel angleTextLabel;
    private javax.swing.JLabel distLabel;
    private javax.swing.JLabel distTextLabel;
    private javax.swing.JLabel xCoordLabel;
    private javax.swing.JLabel xTextLabel;
    private javax.swing.JLabel yCoordLabel;
    private javax.swing.JLabel yTextLabel;
    // End of variables declaration//GEN-END:variables

    
}
